 //
//  DWJStatusCell.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import SnapKit

class DWJStatusCell: UITableViewCell {

    
    // 记录底部toolBar的约束
    var toolbarTopConstraint: Constraint?
    
    var status: DWJStatus? {
    
        didSet{
            
            // 给原创微博赋值
            originalView.status = status
            
            // 在重新给toolBar设置约束之前 需要先卸载以前的约束 
            self.toolbarTopConstraint?.uninstall()
            
            if status?.retweeted_status == nil {
            
                // 没有转发微博
                retweetView.hidden = true
                
                toolBar.snp_makeConstraints(closure: { (make) -> Void in

                    toolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint

                })
                
            } else {
            
                // 有转发微博
                retweetView.hidden = false
                
                retweetView.status = status

                
                toolBar.snp_makeConstraints(closure: { (make) -> Void in

                    toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }
            
            // 转发评论赞的数据
            toolBar.status = status
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // MARK: -  设置视图
    private func setUpUI() {
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
    
        // 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        // 设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView).offset(8) 
            make.left.right.equalTo(contentView)
            // 已经通过文字底部判断了
//            make.height.equalTo(50)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(37)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)

        }
        
        // 设置cell的高度
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(toolBar)
            make.left.right.top.equalTo(self)
        }
        
        
    }
    
    
    
    // MARK: - 懒加载
    // 原创微博
    private lazy var originalView: DWJStatusOriginalView = {
    
        let v = DWJStatusOriginalView()
        
        return v
    }()
    
    // 底部转发评论赞
    private lazy var toolBar: DWJStatusToolbar = {
    
        let tool = DWJStatusToolbar()
        
        return tool
    }()
    
    // 转发微博
    private lazy var retweetView: DWJRetweetView = {
    
        let v = DWJRetweetView()
        
        
        return v
    }()
    
}
